package com.afsadmedia.pregnancy;

import java.util.ArrayList;
import java.util.List;

import com.origamilabs.library.views.StaggeredGridView;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.TabActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.provider.MediaStore;
import android.util.Log;
import android.view.Gravity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.FrameLayout.LayoutParams;
import android.widget.TabHost.TabSpec;
import android.widget.Toast;

public class Notes extends Activity {

	TabHost mTabHost;

	ImageView imgnote, imgcheklist, imgmic, imgcamera;
	private static final int CAMERA_REQUEST = 1;

	private static int RESULT_LOAD_IMAGE = 1;

	private String urls[] = null;
		
//		{
//			"http://farm7.staticflickr.com/6101/6853156632_6374976d38_c.jpg",
//			"http://farm8.staticflickr.com/7232/6913504132_a0fce67a0e_c.jpg",
//			"http://farm5.staticflickr.com/4133/5096108108_df62764fcc_b.jpg" };
	
	
	

	List<ListItemSymptomSelect> rowItems;
	CustomGridViewAdapter adapter;
	CustomGridViewAdaptercheklist adapter1;

	ArrayList<String> datalist;

	CheckBox check;
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.note);

		// ////////////////////////////////////////////////////

		Constants.asiii.add("ASCASD");
		Constants.asiii.add("PSWEWEF");
		Constants.asiii.add("RSEFQWE");

		StaggeredGridView gridView = (StaggeredGridView) this
				.findViewById(R.id.staggeredGridView1);

		int margin = getResources().getDimensionPixelSize(R.dimen.margin);

		gridView.setItemMargin(margin); // set the GridView margin

		gridView.setPadding(margin, 0, margin, 0); // have the margin on the
													// sides as well

		
//		if(Constants.addnotearry != null){
//			
//			rowItems = new ArrayList<ListItem>();
//			for (int i = 0; i < Constants.addnotearry.size(); i++) {
//
//				ListItem item1 = new ListItem(Constants.addnotearry.get(i), null);
//
//				rowItems.add(item1);
//			}
//
////			adapter = new CustomGridViewAdapter(getApplicationContext(),
////					R.layout.gridtext, rowItems);
////
////			gridView.setAdapter(adapter);
////
////			adapter.notifyDataSetChanged();
//			
//		}
		
		if (Constants.addnotearry!=null){
				Log.v("Front",Constants.addnotearry.toString());
			
			rowItems = new ArrayList<ListItemSymptomSelect>();
			for (int i = 0; i < Constants.addnotearry.size(); i++) {

				ListItemSymptomSelect item1 = new ListItemSymptomSelect(check,Constants.addnotearry.get(i));

				rowItems.add(item1);
			}
			
			if(Constants.ashish == "Note"){

			adapter1 = new CustomGridViewAdaptercheklist(getApplicationContext(),
			R.layout.gridtext, rowItems);

	        gridView.setAdapter(adapter1);
            adapter1.notifyDataSetChanged();
			
			Toast.makeText(getApplicationContext(), "Note", Toast.LENGTH_LONG).show();
			}else{
				
				
				adapter1 = new CustomGridViewAdaptercheklist(getApplicationContext(),
						R.layout.gridcheklist, rowItems);

				        gridView.setAdapter(adapter1);
			            adapter1.notifyDataSetChanged();
				Toast.makeText(getApplicationContext(), "Checklist", Toast.LENGTH_LONG).show();
				
			}
			
		}
		
		//else if (){
//		StaggeredAdapter adapter1 = new StaggeredAdapter(Notes.this,
//				R.id.imageView1, R.id.textView_asi, urls);
//		gridView.setAdapter(adapter1);
//
//		adapter1.notifyDataSetChanged();
	//	}
		
		
	

		// /////////////////////////////////////////////////////////////////////

		imgnote = (ImageView) findViewById(R.id.note);
		imgcheklist = (ImageView) findViewById(R.id.cheklist);
		imgmic = (ImageView) findViewById(R.id.mic);
		imgcamera = (ImageView) findViewById(R.id.camera);

		imgnote.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub

				// Handler handler = new Handler();
				// handler.postDelayed(new Runnable() {
				// public void run() {
				//
				//
				// }
				// }, 100);

				// imgnote.setBackgroundColor(Color.parseColor("#CCCCCD"));

				Intent i = new Intent(Notes.this, AddNote.class);
				startActivity(i);

			}
		});

		imgcheklist.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Intent i = new Intent(Notes.this, NoteList.class);
				startActivity(i);

			}
		});

		imgmic.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Intent i = new Intent(Notes.this, MicActivity.class);
				startActivity(i);

			}
		});

		imgcamera.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub

				showPopUP();
			}
		});

	}

	private void showPopUP() {

		AlertDialog.Builder myDialog = new AlertDialog.Builder(Notes.this);
		myDialog.setTitle("  Add Photo  ");

		Button button = new Button(Notes.this);

		Button button2 = new Button(Notes.this);

		// EditText editText = new EditText(MainActivity.this);
		TextView textView = new TextView(Notes.this);

		TextView textView1 = new TextView(Notes.this);

		button.setBackgroundResource(R.drawable.ic_camera);

		myDialog.setNegativeButton("Cancel", null);

		button2.setBackgroundResource(R.drawable.galleryicon);

		textView.setTextSize(20);
		textView1.setTextSize(20);

		LayoutParams buttonLayoutParams = new LayoutParams(
				LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		buttonLayoutParams.setMargins(100, 100, 50, 0);

		final LinearLayout layout22 = new LinearLayout(Notes.this);
		layout22.setOrientation(LinearLayout.HORIZONTAL);

		final LinearLayout layout33 = new LinearLayout(Notes.this);
		layout33.setOrientation(LinearLayout.HORIZONTAL);

		layout22.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub

				layout22.setBackgroundColor(Color.parseColor("#999999"));

				Intent cam = new Intent(
						android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
				startActivityForResult(cam, CAMERA_REQUEST);

			}
		});

		layout33.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				layout33.setBackgroundColor(Color.parseColor("#999999"));
				Intent i = new Intent(
						Intent.ACTION_PICK,
						android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

				startActivityForResult(i, RESULT_LOAD_IMAGE);

			}
		});

		button.setLayoutParams(buttonLayoutParams);
		textView.setTextColor(Color.BLACK);
		textView.setText("     Take a Snap        ");

		textView1.setTextColor(Color.BLACK);
		textView1.setText("     Gallery                   ");

		// String resetPassword = editText.getText().toString();
		LinearLayout layout2 = new LinearLayout(Notes.this);
		layout2.setOrientation(LinearLayout.VERTICAL);

		layout22.setPadding(1, 20, 1, 5);
		layout22.setBackgroundColor(Color.WHITE);
		layout22.setOrientation(LinearLayout.HORIZONTAL);
		layout22.addView(textView);
		layout22.addView(button);

		layout33.setPadding(1, 20, 1, 5);
		layout33.setBackgroundColor(Color.WHITE);
		layout33.setOrientation(LinearLayout.HORIZONTAL);
		layout33.addView(textView1);
		layout33.addView(button2);

		LinearLayout layout1 = new LinearLayout(Notes.this);

		layout1.setPadding(1, 20, 1, 10);
		layout1.setGravity(Gravity.CENTER_HORIZONTAL);

		layout2.addView(layout22);
		layout2.addView(layout33);

		layout2.addView(layout1);

		myDialog.setView(layout2);
		// myDialog.setView(layout1);

		myDialog.show();

	}

	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);

		if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK
				&& null != data) {
			Uri selectedImage = data.getData();
			String[] filePathColumn = { MediaStore.Images.Media.DATA };

			Cursor cursor = getContentResolver().query(selectedImage,
					filePathColumn, null, null, null);
			cursor.moveToFirst();

			int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
			String picturePath = cursor.getString(columnIndex);
			cursor.close();

			Constants.gallery = BitmapFactory.decodeFile(picturePath);
			Intent i = new Intent(Notes.this, Camera.class);
			startActivity(i);

		} else if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK
				&& null != data) {
			if (resultCode == -1) {

				Constants.photo = (Bitmap) data.getExtras().get("data");

				Intent i = new Intent(Notes.this, Camera.class);
				startActivity(i);
			}

		}

	}
	
	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		Intent i = new Intent(Notes.this,Listview.class);
		startActivity(i);
		finish();
		super.onBackPressed();
	}

}
